import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score
%matplotlib inline

data = pd.read_csv("Desktop/Bike-Sharing-Dataset/day.csv")

#直观上感受，体感温度比室外温度更能够影响用户的骑车意愿，所以去掉temp特征，保留atemp特征
data = data.drop('temp',axis=1)

#去除cnt离群点
data = data[data.cnt<8000]
data = data[data.cnt>50]

#去除hum离群点
data = data[data.hum>0.3]

train = data[data.yr==0].drop('yr',axis=1)
test = data[data.yr==1].drop('yr',axis=1)
x_train = train.drop('cnt',axis=1)
x_test = test.drop('cnt',axis=1)
y_train = train['cnt']
y_test = test['cnt']


#对特征做归一化
from sklearn.preprocessing import MinMaxScaler
mms = MinMaxScaler()
x_train = mms.fit_transform(x_train)
x_test = mms.transform(x_test)

#对y_test和y_train分开做标准化
from sklearn.preprocessing import StandardScaler
ss_train = StandardScaler()
ss_test = StandardScaler()
y_train = ss_train.fit_transform(y_train.reshape(-1,1))
y_test = ss_train.fit_transform(y_test.reshape(-1,1))

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(x_train,y_train)

y_train_pred = lr.predict(x_train)
y_test_pred = lr.predict(x_test)

print("最小二乘线性回归训练集r2_score评分：",r2_score(y_train,y_train_pred))
print("最小二乘线性回归测试集r2_score评分：",r2_score(y_test,y_test_pred))

#增加独热编码
x_train = train.drop('cnt',axis=1)
x_test = test.drop('cnt',axis=1)
y_train = train['cnt']
y_test = test['cnt']

x_train_type = x_train.drop(['hum','atemp','windspeed'],axis=1)
x_train_num = x_train[['hum','atemp','windspeed']]
x_test_type =  x_test.drop(['hum','atemp','windspeed'],axis=1)
x_test_num = x_test[['hum','atemp','windspeed']]

from sklearn.preprocessing import MinMaxScaler
mms = MinMaxScaler()
x_train_num = mms.fit_transform(x_train_num)
x_test_num = mms.transform(x_test_num)

from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder()
x_train_type = enc.fit_transform(x_train_type).toarray()
x_test_type = enc.transform(x_test_type).toarray()

x_train = np.concatenate((x_train_type,x_train_num),axis=1)
x_test = np.concatenate((x_test_type,x_test_num),axis=1)

from sklearn.preprocessing import StandardScaler
ss_y_train = StandardScaler()
ss_y_test = StandardScaler()
y_train = ss_y_train.fit_transform(y_train.reshape(-1,1))
y_test = ss_y_test.fit_transform(y_test.reshape(-1,1))

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(x_train,y_train)

y_train_pred = lr.predict(x_train)
y_test_pred = lr.predict(x_test)

print("增加独热编码后最小二乘线性回归训练集r2_score评分：",r2_score(y_train,y_train_pred))
print("增加独热编码后最小二乘线性回归测试集r2_score评分：",r2_score(y_test,y_test_pred))

#岭回归
from sklearn.linear_model import RidgeCV

alphas = [0.01,0.1,1,10]
lr = RidgeCV(alphas=alphas,store_cv_values=True)
lr.fit(x_train,y_train)

y_train_pred = lr.predict(x_train)
y_test_pred = lr.predict(x_test)

print("岭回归训练集r2_score评分：",r2_score(y_train,y_train_pred))
print("岭回归测试集r2_score评分：",r2_score(y_test,y_test_pred))

#Lasso回归
from sklearn.linear_model import LassoCV

alphas = [0.01,0.1,1,10,100]
ls =LassoCV(alphas=alphas)
ls.fit(x_train,y_train)

y_train_pred = ls.predict(x_train)
y_test_pred = ls.predict(x_test)

print(r2_score(y_train,y_train_pred))